package com.xiaohudie.JDBC;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Author xiao hu die
 * @Date 2022/3/13 13:43
 * @Version 1.0
 */
/**preparestatement
 * 实现模糊查询*/
public class TestPreStatement {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties =new Properties();
        properties.load(new FileReader("src//dp.properties"));
        String driver = properties.getProperty("driver");
        String user = properties.getProperty("user");
        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        /**注册驱动*/
        try {
            Class.forName(driver);
            /**获取连接*/
            connection = DriverManager.getConnection(url, user, password);
            /**定义sql语句*/
            String SelectSql ="SELECT id FROM user where name like ?";
            /**先编译*/
             preparedStatement = connection.prepareStatement(SelectSql);
             /**再赋值*/
             preparedStatement.setString(1,"%陈%");
             /**再执行*/
             resultSet=  preparedStatement.executeQuery();
             while (resultSet.next()){
                 int id = resultSet.getInt("id");
                 System.out.println("id为-------》"+id);
             }
        }finally {
            if (resultSet!=null){
                resultSet.close();
            }
           if (preparedStatement!=null){
               preparedStatement.close();
           }
           if (connection!=null){
               connection.close();
           }
        }
    }
}
